package cn.nfwx.tv.adapter;

import android.support.annotation.LayoutRes;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import cn.nfwx.tv.R;
import cn.nfwx.tv.model.SubscriptModel;
import cn.nfwx.tv.model.TvProgram;
import cn.nfwx.tv.util.GlideUtil;
import cn.nfwx.tv.widget.ItemHotColumn;

/**
 * Created by dell on 2017/11/13.
 */

public class MineSubcriptionAdapter2 extends BaseQuickAdapter<Object, BaseViewHolder> {

    public static final int ITEM_TITLE = 0;
    public static final int ITEM_PART1 = 1;
    public static final int ITEM_PART2 = 2;

    private SparseArray<Integer> layouts;


    public MineSubcriptionAdapter2() {
        super(null);
        addItemType(ITEM_TITLE, R.layout.fragment_hot_column_item);
        addItemType(ITEM_PART1, R.layout.item_subcription_part1_layout);
        addItemType(ITEM_PART2, R.layout.item_subsription_loadmore_layout);
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        return createBaseViewHolder(parent, getLayoutId(viewType));
    }

    private int getLayoutId(int viewType) {
        return layouts.get(viewType);
    }

    protected void addItemType(int type, @LayoutRes int layoutResId) {
        // layouts
        if (layouts == null) {
            layouts = new SparseArray<>();
        }
        layouts.put(type, layoutResId);
    }

    @Override
    protected int getDefItemViewType(int position) {
        Object object = getData().get(position);
        if (object instanceof SubscriptModel) {
            return ITEM_TITLE;
        } else if (object instanceof TvProgram) {
            return ITEM_PART1;
        } else if (object instanceof SubscriptModel.LoadMore) {
            return ITEM_PART2;
        }
        return ITEM_TITLE;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final Object object) {
        if (object instanceof SubscriptModel) {
            SubscriptModel data = (SubscriptModel) object;
            helper.setText(R.id.program_name, data.getSubject_name());
            helper.setVisible(R.id.hot_column_content, false);
            GlideUtil.loadCircleImage(helper.itemView.getContext(), data.getSnap(), (ImageView) helper.getView(R.id.program_image));
            if (data.isFousc()) {
                helper.setVisible(R.id.subscription_btn, false);
                helper.setVisible(R.id.subscription_cancle_btn, true);
            } else {
                helper.setVisible(R.id.subscription_btn, true);
                helper.setVisible(R.id.subscription_cancle_btn, false);
            }
            ItemHotColumn hot_channel = helper.getView(R.id.hot_channel);
            hot_channel.setText(data.getChannel());
            ItemHotColumn hot_language = helper.getView(R.id.hot_language);
            hot_language.setText(data.getLanguage());
            ItemHotColumn hot_time = helper.getView(R.id.hot_time);
            hot_time.setText(data.getTime());
            helper.setText(R.id.hot_descrition, data.getDescription());
            helper.getView(R.id.ll_item_search).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (helper.getView(R.id.hot_column_content).getVisibility() == View.GONE) {
                        helper.setVisible(R.id.hot_column_content, true);
                    } else if (helper.getView(R.id.hot_column_content).getVisibility() == View.VISIBLE) {
                        helper.setVisible(R.id.hot_column_content, false);
                    }
                }
            });
            helper.getView(R.id.up_btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    helper.setVisible(R.id.hot_column_content, false);
                }
            });
        } else if (object instanceof TvProgram) {
            TvProgram data = (TvProgram) object;
            helper.setText(R.id.video_name, data.getProgram_name() + "_" + data.getTime());
            GlideUtil.loadRoundImage(helper.getConvertView().getContext(), data.getSnap(), (ImageView) helper.getView(R.id.iv_subscription));
            helper.setText(R.id.video_time, data.getTime());
        } else if (object instanceof SubscriptModel.LoadMore) {
            SubscriptModel.LoadMore data = (SubscriptModel.LoadMore) object;
            if (data.isLoadMore) {
                helper.setText(R.id.tv_load_more, "收起更多");
            } else {
                helper.setText(R.id.tv_load_more, "显示更多");
            }
        }
        helper.addOnClickListener(R.id.subscription_cancle_btn);
        helper.addOnClickListener(R.id.subscription_btn);
    }


}
